// var mongoAdminNewsList = require('../../models/admin/news').news;
// var async = require('async');
// exports.newsList = function(req, res, next){
//     async.parallel({
//         count : function(callback){
//             mongoAdminNewsList.count(function(e, body){
//                 if(e){

//                 }else{
//                     callback(null, body)
//                 }
//             })
//         },
//         list : function(callback){
//             mongoAdminNewsList.find(function(e, body){
//                 if(e){

//                 }else{
//                     callback(null, body)
//                 }
//             }).limit(20);
//         }
//     },function(e,results){
//         res.render('admin/newslist',{
//             results : results.list,
//             count : results.count
//         });
//     })
// };
var mysql = require('../../dbs/mysql').connection;
var async = require('async');
exports.newsList = function(req,res,next){
  async.auto({
    count : function(callback){
      var sql = 'SELECT COUNT(*) AS total FROM news';
      mysql.query(sql, function(err, results){
        var dataModel = {
          total : 0
        }
        if(err){
            callback(null, dataModel)
        }else{
          dataModel.total = results[0].total;
            callback(null, dataModel)
        }
      })
    },
    list : function(callback){
      var sql = 'SELECT * FROM news limit 10';
      mysql.query(sql, function(err, results){
        if(err){
            callback(null, { })
        }else{
            callback(null, results)
        }
      })
    }
  },function(err, results){
    console.log(results,'--------')
    res.render('admin/newslist',{
      "results" : results.list,
      "count" : results.count
    })
  })
};
exports.checkUser = function(req,res,next){
  var pw = md5(req.body.passwd);
  var userName = req.body.username;
  mysql.query('SELECT * FROM adminuser where username ="' + userName + '"',function(err,results){
    if(err){
      console.log('ERROR:' + err,'========checkUser=======')
      res.send({ "message" : err, code : 410 });
    }else{
      if(results[0].passwd == pw){
        next();
      }else{
        res.send({'message':'密码错误',code : 400});
      }
    };
  });
};
exports.loginSuccess = function(req,res,next){
  //res.redirect('/admin');
  res.send('ok');
};